package com.nuoniu.sibanyun.mapper.erp.in;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nuoniu.sibanyun.entity.bo.UnExecutedBo;
import com.nuoniu.sibanyun.entity.erp.in.*;
import com.nuoniu.sibanyun.entity.vo.InInventoryPropertyVo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 物料档案 Mapper 接口
 * </p>
 *
 * @author baiqiang
 * @since 2020-09-22
 */
public interface InInventoryMapper extends BaseMapper<InInventory> {

    @Select("select * from scb_material_archive where inventory_class_id = #{id}")
    List<InInventory> getByClass(Integer id);

    /**
     * 修改仓库名称
     * @author 小啊刚
     * @date 2020-09-30
     */
    @Update("update scb_material_archive set warehouse_name = #{whName} where warehouse_id = #{id}")
    void updateWarehouse(InWarehouse inWarehouse);

    /**
     * 根据code获取
     * @author 小啊刚
     * @date 2020-09-30
     */
    @Select("select * from scb_material_archive where (inventory_code=#{inventoryCode}) and company_id=#{companyId}")
    InInventory getByCode(InInventory inInventory);

    /**
     * 根据名称获取
     * @author 小啊刚
     * @date 2020-09-30
     */
    @Select("select * from scb_material_archive where (inventory_name=#{inventoryName}) and company_id=#{companyId} and inventory_specification=#{inventorySpecification}")
    InInventory getByName(InInventory inInventory);



    /**
     * 根据计量单位id获取物料
     * @author 小啊刚
     * @date 2020-11-24
     */
    @Select("select * from scb_material_archive where unit_id=#{id} and company_id=#{companyId} and unit_type='单计量'")
    List<InInventory> getByUnitId(Integer id, Integer companyId);

    /**
     * 根据计量单位组获取物料
     * @author 小啊刚
     * @date 2020-11-24
     */
    @Select("select * from scb_material_archive where unit_group_id=#{unitGroupId} and company_id=#{companyId} and unit_type='多计量'")
    List<InInventory> getByUnitGroupId(Integer unitGroupId, Integer companyId);

    @Select("select count(1) from scb_material_archive where unit_id=#{id} or pro_unit=#{id} or main_unit=#{id} or  main_unit=#{id} or pur_unit=#{id} or sale_unit=#{id} " +
            " or retail_unit=#{id} or cost_unit=#{id} or sub_unit_by_report=#{id} or in_storage_up_limit=#{id}")
    Integer getByUnit(Long id);

    @Update("update ${tableName} set inventory_name=#{inventoryName}, inventory_specification=#{specification} where inventory_id=#{inventoryId}")
    void updateBillInventory(String tableName, Long inventoryId, String inventoryName, String specification);

    @Select("select count(1) from ${tableName} where inventory_id in (${ids})")
    Integer getBillInventory(String tableName, String ids);

    @Select("select id from scb_material_archive where warehouse_id in (${warehouseIds})")
    List<InInventory> getByWarehouse(String warehouseIds);

    /**
     * 获取物料未执行数量情况
     * @param companyId
     * @param accountId
     * @param inventoryIds
     * @return
     */
    List<UnExecutedBo> getUnExecuteQuantityList(String mainTableName, String detailTableName, Integer companyId, Integer accountId ,String inventoryIds,String warehouseIds);

    /**
     * 获取 某些物料的现存量（合并不同仓库）
     * @param companyId
     * @param accountId
     * @param inventoryIds
     * @return
     */
    List<InInventoryInfo> listExistingQuantity(Integer companyId, Integer accountId , String inventoryIds);

    @Update("update scb_material_archive set inventory_class_name=#{treeNames} where inventory_class_id=#{id}")
    void updateInventoryClass(InInventoryClass inInventoryClass);

    @Select("SELECT code, `name` FROM pub_voucher_design WHERE company_id = #{companyId} AND account_id = #{accountId}  AND source = 'Inventory' AND `status`= 0 ANd on_off=1 and data_type=1 ORDER BY `data_tab` ASC,sort ASC;")
    List<Map<String, String>> getInventoryImportFiled(Integer companyId, Integer accountId);

    @Select("select concat(inventory_name, inventory_specification) from scb_material_archive where company_id = #{companyId} and status=0")
    String[] getByInventoryNames(Integer companyId);

    /**
     * 获取当前分类最大编码
     * @author 小啊刚
     * @date 2021-06-18
     */
    @Select("select  left(inventory_code, #{strLength})inventory_code from scb_material_archive where inventory_class_id=#{classId} order by inventory_code desc limit 1 ")
    String getMaxCodeByClassId(Integer classId, Integer strLength);

    @Select("select inventory_code from scb_material_archive where company_id=#{companyId} and status=0 and inventory_class_id=#{classId}")
    String[] getInventoryCodes(Integer companyId, Integer classId);

    /**
     * 根据分类获取物料
     * @author 小啊刚
     * @date 2021-07-16
     */
    List<HashMap<String, Object>> getInventoryList(Integer companyId,  String classCode);

    @Update("update scb_material_archive set latest_cost=#{latestCost} where id=#{id}")
    void updatePrice(InInventory inventory);


    InInventoryPropertyVo getInInventoryPropertyVo(Integer id);

    /**
     * 获取商品属性
     * @param companyId
     * @param id
     * @return
     */
    String getCommodityAttribute(Integer companyId,Long id);

    @Delete("delete from ${tableName} where company_id=#{i}")
    void deleteOrder(String tableName, int i);
}
